home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
C/C++ Users Group Library 1996 July
/
C-C++ Users Group Library July 1996.iso
/
listings
/
v_11_08
/
1108074a
< prev
next >
Wrap
Text File
|
1993-06-06
|
27KB
|
867 lines
/*******************************************
*
* special_opening(...
*
* Opening is erosion followed by dilation.
* This routine will use the thinning
* erosion routine. This will not allow
* an object to erode to nothing.
*
* The number parameter specifies how
* erosions to perform before doing one
* dilation.
*
*******************************************/
special_opening(in_name, out_name, the_image,
out_image, il, ie, ll, le,
value, threshold, number)
char in_name[], out_name[];
int il, ie, ll, le, number;
short the_image[ROWS][COLS],
out_image[ROWS][COLS],
threshold, value;
{
int a, b, count, i, j, k;
int length, width;
struct tiff_header_struct image_header;
if(does_not_exist(out_name)){
printf("\n\n output file does not exist %s", out_name);
read_tiff_header(in_name, &image_header);
round_off_image_size(&image_header,
&length, &width);
image_header.image_length = length*ROWS;
image_header.image_width = width*COLS;
create_allocate_tiff_file(out_name, &image_header,
out_image);
} /* ends if does_not_exist */
read_tiff_image(in_name, the_image, il, ie, ll, le);
thinning(in_name, out_name, the_image,
out_image, il, ie, ll, le,
value, threshold, 1);
if(number > 1){
count = 1;
while(count < number){
count++;
thinning(out_name, out_name, the_image,
out_image, il, ie, ll, le,
value, threshold, 1);
} /* ends while */
} /* ends if number > 1 */
dilation(out_name, out_name, the_image,
out_image, il, ie, ll, le,
value, threshold);
write_array_into_tiff_image(out_name, out_image,
il, ie, ll, le);
} /* ends special_opening */
/*******************************************
*
* thinning(...
*
* Use a variation of the grass fire
* wave front approach.
*
* Raster scan the image left to right
* and examine and thin the left edge pixels
* (a 0 to value transition). Process them
* normally and "save" the result. Next,
* raster scan the image right to left and
* save. Raster scan top to bottom and save.
* Raster scan bottom to top and save.
*
* That is one complete pass.
*
* Keep track of pixels thinned for a
* pass and quit when you make a complete
* pass without thinning any pixels.
*
*******************************************/
thinning(in_name, out_name, the_image, out_image,
il, ie, ll, le, value, threshold, once_only)
char in_name[], out_name[];
int il, ie, ll, le, once_only;
short the_image[ROWS][COLS],
out_image[ROWS][COLS],
threshold, value;
{
int a, b, big_count, count, i, j, k,
not_finished;
int length, width;
struct tiff_header_struct image_header;
if(does_not_exist(out_name)){
printf("\n\n output file does not exist %s", out_name);
read_tiff_header(in_name, &image_header);
round_off_image_size(&image_header,
&length, &width);
image_header.image_length = length*ROWS;
image_header.image_width = width*COLS;
create_allocate_tiff_file(out_name, &image_header,
out_image);
} /* ends if does_not_exist */
read_tiff_image(in_name, the_image, il, ie, ll, le);
for(i=0; i<ROWS; i++)
for(j=0; j<COLS; j++)
out_image[i][j] = the_image[i][j];
not_finished = 1;
while(not_finished){
if(once_only == 1)
not_finished = 0;
big_count = 0;
/***************************
*
* Scan left to right
* Look for 0-value transition
*
****************************/
printf("\n");
for(i=1; i<ROWS-1; i++){
if( (i%10) == 0) printf("%3d", i);
for(j=1; j<COLS-1; j++){
if(the_image[i][j-1] == 0 &&
the_image[i][j] == value){
count = 0;
for(a=-1; a<=1; a++){
for(b=-1; b<=1; b++){
if(the_image[i+a][j+b] == 0)
count++;
} /* ends loop over b */
} /* ends loop over a */
if(count > threshold){
if(can_thin(the_image, i, j, value)){
out_image[i][j] = 0;
big_count++;
} /* ends if can_thin */
} /* ends if count > threshold */
} /* ends if the_image == value */
} /* ends loop over j */
} /* ends loop over i */
/**************************************
*
* Copy the output back to the input.
*
**************************************/
for(i=0; i<ROWS; i++)
for(j=0; j<COLS; j++)
the_image[i][j] = out_image[i][j];
/***************************
*
* Scan right to left
* Do this by scanning left
* to right and look for
* value-0 transition.
*
****************************/
printf("\n");
for(i=1; i<ROWS-1; i++){
if( (i%10) == 0) printf("%3d", i);
for(j=1; j<COLS-1; j++){
if(the_image[i][j+1] == 0 &&
the_image[i][j] == value){
count = 0;
for(a=-1; a<=1; a++){
for(b=-1; b<=1; b++){
if(the_image[i+a][j+b] == 0)
count++;
} /* ends loop over b */
} /* ends loop over a */
if(count > threshold){
if(can_thin(the_image, i, j, value)){
out_image[i][j] = 0;
big_count++;
} /* ends if can_thin */
} /* ends if count > threshold */
} /* ends if the_image == value */
} /* ends loop over j */
} /* ends loop over i */
/**************************************
*
* Copy the output back to the input.
*
**************************************/
for(i=0; i<ROWS; i++)
for(j=0; j<COLS; j++)
the_image[i][j] = out_image[i][j];
/***************************
*
* Scan top to bottom
* Look for 0-value transition
*
****************************/
printf("\n");
for(j=1; j<COLS-1; j++){
if( (j%10) == 0) printf("%3d", j);
for(i=1; i<ROWS-1; i++){
if(the_image[i-1][j] == 0 &&
the_image[i][j] == value){
count = 0;
for(a=-1; a<=1; a++){
for(b=-1; b<=1; b++){
if(the_image[i+a][j+b] == 0)
count++;
} /* ends loop over b */
} /* ends loop over a */
if(count > threshold){
if(can_thin(the_image, i, j, value)){
out_image[i][j] = 0;
big_count++;
} /* ends if can_thin */
} /* ends if count > threshold */
} /* ends if the_image == value */
} /* ends loop over i */
} /* ends loop over j */
/**************************************
*
* Copy the output back to the input.
*
**************************************/
for(i=0; i<ROWS; i++)
for(j=0; j<COLS; j++)
the_image[i][j] = out_image[i][j];